package com.zj.user.controller;

import com.zj.user.domain.User;
import com.zj.user.service.UserService;
import com.zj.user.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: zj
 * @Date: 2018/11/23 00:49
 * @Description:
 */
@RestController
public class ServiceController {
    @Autowired
    UserService userService;
    @Autowired
    JwtTokenUtil jwtTokenUtil;

    //通过username查找user
    @GetMapping("/getUserByUsername")
    public User getUserByUsername(@RequestParam String username){
        return userService.findOneUserByUsername( username );
    }

    //从token中获取username
    @GetMapping("/getUsernameFromToken")
    public String getUsernameFromToken(@RequestParam String authToken) {
        return jwtTokenUtil.getUsernameFromToken( authToken );
    }
    //从token中获取user role
    @GetMapping("/getUserRoleFromToken")
    public String getUserRoleFromToken(@RequestParam String authToken) {
//        System.out.println( "getUserRoleFromToken 成功" );
        return jwtTokenUtil.getUserRoleFromToken( authToken );
    }
    //通过id查找user
    @GetMapping("/getUserById")
    public User getUserById(@RequestParam Integer id) {
        return userService.findOneUserById( id );
    }

    //保存或更新数据库
    @GetMapping("/UpdateUserWebsocketSessionId")
    public void SaveUser(@RequestParam Integer id,@RequestParam String WebsocketSessionId) {
        User user = userService.findOneUserById( id );
        user.setWebsocketSessionId( WebsocketSessionId );
        userService.UpDate( user );
    }

    //验证token
    @GetMapping("/validateToken")
    public Boolean validateToken(@RequestParam String authToken,
                                 @RequestParam User user) {
        return jwtTokenUtil.validateToken( authToken,user );
    }
    //获取所有的用户
    @GetMapping("/getAllUser")
    public List<User> getAllUser() {
        return userService.selectAllUser();
    }

}
